<?php

namespace Web\MainSiteBundle\Controller;

use Model\SchoolBundle\Model\SchoolApplyModel;
use Model\SchoolBundle\Model\SchoolModel;
use Model\SchoolCategoryBundle\Model\SchoolCategoryModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;

/**
 * @route("/School/")
 */
class SchoolApplyController extends WebBaseController
{
    /**
     * @route("add",name="WebSchoolApply")
     */
    public function WebSchoolApply(Request $request)
    {
        $user = $this->needToBeUser();
        $model = new SchoolModel($this->getDoctrine());
        if ( $request->getMethod() == "POST") {//新增
            //获取学校信息
            if (!($type = $this->getParamRequired("type")) && is_bool($this->getParamRequired("type"))) {
                return $this->msg_out("机构性质未设置");
            }
            if (!$category = $this->getParamRequired("category")) {
                return $this->msg_out("学校分类未设置");
            }
            if (!$name = $this->getParamRequired("name")) {
                return $this->msg_out("学校名称未设置");
            }
            if (!$logo = $this->getParamRequired("logoPic")) {
                return $this->msg_out("学校LOGO未设置");
            }
            if (!$intro = $this->getParamRequired("intro")) {
                return $this->msg_out("学校简介未设置");
            }
            $params = array(
                "type"          => $type,//0=机构，1=个人
                "userId"        => $user->getId(),
                "category"      => $category,//SchoolCategory
                "name"          => $name,
                "logo"          => $logo,
                "intro"         => $intro,
                "addTime"       => time(),
                "params"        => "[]",//Json 字符串
                "status"        => 0,//0=未审核，1=已审核
            );

            //获取校长（申请人）信息
            if (!$pName = $this->getParamRequired("pName")) {$this->msg_out("联系人姓名未填写");}
            if (!$pPhone = $this->getParamRequired("pPhone")) {$this->msg_out("联系人手机未填写");}
            if (!$pEmail = $this->getParamRequired("pEmail")) {$this->msg_out("联系人邮箱未填写");}
            if (!$pQQ = $this->getParamRequired("pQQ")) {$this->msg_out("联系人QQ未填写");}
            if (!$pIdCard = $this->getParamRequired("pIdCard")) {$this->msg_out("联系人身份证号未填写");}
            if (!$pIdCardPic = $this->getParamRequired("pIdCardPic")) {$this->msg_out("联系人身份证照片未提交");}
            if (is_array($pIdCardPic)) {
                $pIdCardPic = $pIdCardPic[0];
            }
            $manager = array(
                "pName"     => $pName,
                "pPhone"    => $pPhone,
                "pEmail"    => $pEmail,
                "pQQ"       => $pQQ,
                "pIdCard"   => $pIdCard,
                "pIdCardPic"=> $pIdCardPic
            );

            //检查机构类型
            if ($type == 1) {//个人
                $params["params"] = json_encode(array("manager"=>$manager));
            } else {//机构
                if (!$oName = $this->getParamRequired("oName")) {$this->msg_out("机构名未填写");}
                if (!$oType = $this->getParamRequired("oType")) {$this->msg_out("机构类型未填写");}
                if (!$oSite = $this->getParamRequired("oSite")) {$this->msg_out("机构网址未填写");}
                if (!$oWebo = $this->getParamRequired("oWebo")) {$this->msg_out("机构微博未填写");}
                if (!$oCode = $this->getParamRequired("oCode")) {$this->msg_out("机构代码未填写");}
                if (!$oCodePic = $this->getParamRequired("oCodePic")) {$this->msg_out("机构代码图未填写");}
                $organization = array(
                    "oName" => $oName,
                    "oType" => $oType,
                    "oSite" => $oSite,
                    "oWebo" => $oWebo,
                    "oCode" => $oCode,
                    "oCodePic"  => $oCodePic,
                );
                $params["params"] = json_encode(array("manager"=>$manager,"organization"=>$organization));
            }
            $rst = $model->addNewApply($params);
            if ($rst) {
                return $this->msg_out("信息已提交请耐心等待",$this->generateUrl("WebSchoolApply"));
            } else {
                return $this->msg_out("返回异常信息");
            }
        } else {
            $schoolCategoryModel = new SchoolCategoryModel($this->getDoctrine());
            $schoolCategory = $schoolCategoryModel->getAllSchoolCategory();
            $data["cat"] = $schoolCategory;
            $hasSchool = $model->checkHasSchool($user->getId());
            if (!empty($hasSchool)) {//已经有创建过学校
                return $this->msg_out("您已创建学校，请勿重复申请",$this->generateUrl("WebUserIndex"));
            } else {//暂时没有学校
                //分两种情况：1.未申请；2.申请了但是没通过
                $teacherApplyModel = new SchoolApplyModel($this->getDoctrine());
                $apply = $teacherApplyModel->getByUserId($user->getId());
                if ( isset($apply["params"]) && isset($apply["status"]) ) {//申请已经提交
                    $params = json_decode($apply["params"],true);
                    if ($apply["status"] == 0) {
                        $msg = "您的信息已提交，工作人员将尽快审核，再次提交将覆上次提交的内容。";
                    } else {
                        $msg = isset($params["msg"]) ? $params["msg"] : "您提交的信息可能存在问题，建议您重新提交";
                    }
                    $data["msg"] = $msg;
                }
                return $this->render("WebMainSiteBundle:User:user_apply_school.html.twig",$data);
            }
        }

    }

}
